package com.clstu.socket;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPFileCopyClient {
    public static void main(String[] args) throws IOException {         //特别注意：   ！！！
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);//这里指明的是连接的服务器的ip和端口号
                                                                       //不是自己的，自己（客户端）的端口号是随机分配的（每次都可能不一样），
        //                                                                服务端是8888不变的
        OutputStream outputStream = socket.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream("src\\mkbk.jpg");
        int readLen = 0;
        byte[] buf = new byte[1024];
        while ((readLen = fileInputStream.read(buf))!=-1){//发送图片
            outputStream.write(buf,0,readLen);
        }
        socket.shutdownOutput();
        InputStream inputStream = socket.getInputStream();
        while ((readLen = inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));//接受对方发送的收到消息
        }

        socket.close();
        inputStream.close();
        outputStream.close();
        fileInputStream.close();
        System.out.println("客户端已经退出~~~~~~~~~~~·");
    }
}
